package com.gzdh.dishrecognition.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.provider.Settings;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;

import com.dhkj.web.systemcenter.SysCenterRequestUtils;
import com.dhkj.web.utils.MyObserverRes;
import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.ui.base.BaseMVCActivity;
import com.gzdh.dishrecognition.ui.pre.BindDevicePre;
import com.gzdh.dishrecognition.util.SystemUtils;

import java.math.BigDecimal;
import java.util.Timer;
import java.util.TimerTask;

import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;

public class BindActivity extends BaseMVCActivity {

    TextView deviceSn;

    private ImageView mIvQrcode;

    private BindDevicePre bindDevicePre;
    private Handler mHandler;
    private Runnable mRunnable;

    private TextView tv_title;

    public static void startAct(Context context) {
        Intent intent = new Intent(context, BindActivity.class);
        context.startActivity(intent);
    }

    @Override
    public boolean onKeyEventBack(KeyEvent event) {
        finish();
        return super.onKeyEventBack(event);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.view_bind_device;
    }

    @Override
    protected void initView() {
        mIvQrcode = findViewById(R.id.iv_qrcode);
        deviceSn = findViewById(R.id.deviceSn);
        tv_title = findViewById(R.id.tv_title);
        mHandler = new Handler();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void dissmissPresnetation() {
//        if (bindDevicePre != null) {
//            bindDevicePre.dismiss();
//        }
    }


    @Override
    public void onBusiness() {
        super.onBusiness();
        startTime();
        runOnUiThread(() -> {
            mIvQrcode.setImageBitmap(QRCodeEncoder.syncEncodeQRCode(SystemUtils.getSerialNumber(), 300));
            if(bindDevicePre != null) {
                bindDevicePre.setQr(SystemUtils.getSerialNumber());
            }
        });
        deviceSn.setText(String.format("SN:%s", SystemUtils.getSerialNumber()));
    }

    @Override
    public void initPresentation() {
        super.initPresentation();
        if (mDisplay != null) {
            bindDevicePre = new BindDevicePre(BindActivity.this, mDisplay);
            registerPresentation(bindDevicePre);
            showPresentation(bindDevicePre);
            if(bindDevicePre != null) {
                bindDevicePre.show();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void startTime() {

        mRunnable = new Runnable() {
            @Override
            public void run() {
                tv_title.setText("正在查询");
                SysCenterRequestUtils.init(mActivity, SystemUtils.getSerialNumber(), new MyObserverRes<JSONObject>(mActivity, false) {
                    @Override
                    public void onSuccess(JSONObject result) {
                        System.out.println(result.toJSONString());
                        JSONObject shopMachineJson = result.getJSONObject("shopMachine");
                        JSONObject deviceJson = result.getJSONObject("device");

                        if (shopMachineJson == null || shopMachineJson.isEmpty()) {
                            tv_title.setText("设备未绑定");
                            startTime();
                        } else {
                            SharedPreferences userInfo = getSharedPreferences("sysCost", MODE_PRIVATE);
                            SharedPreferences.Editor editor = userInfo.edit();
                            editor.putString("api", shopMachineJson.getString("businessUrl"));
                            editor.putInt("type", new BigDecimal(deviceJson.getString("type")).intValue());
                            editor.putString("merchantName", shopMachineJson.getString("merchantName"));
                            editor.commit();
                            tv_title.setText("绑定成功,请重新启动");
                        }
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        startTime();
                    }
                });
            }
        };
        mHandler.postDelayed(mRunnable, 3000);
    }


}
